﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BWcsLocSku
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/9 15:13:54
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion
namespace KingerRobot.MexicoSailunWMS.SpiService;

internal class BWcsLocSku : IWcsLocSku
{
    public List<WcsLocSkuBean> GetWcsLocSkuBeans()
    {
        var db = Db.Default;
        var result = db.WcsLocSkus.Join(db.ProdSkuProducts, f => f.SkuId, s => s.SkuId, (f, s) => new
        {
            WcsLocSku = f,
            SkuProduct = s,
        }).Join(db.ProdProducts, f => f.SkuProduct.ProductId, s => s.Id, (f, s) => new WcsLocSkuBean
        {
            POWcsLocSku = f.WcsLocSku,
            POProdProduct = s,
        }).ToList();
        return result;
    }
    public List<POWcsLocSku> GetWcsLocSkuBySkuIds(List<long?> skuIds)
    {
        var db = Db.Default;
        var result = db.WcsLocSkus.Where(t => skuIds.Contains(t.SkuId)).ToList();
        return result;
    }
    public List<POWcsLocSku> GetWcsLocSkuByLocIds(List<long?> locIds)
    {
        var db = Db.Default;
        var result = db.WcsLocSkus.Where(t => locIds.Contains(t.LocId)).ToList();
        return result;
    }
    public POWcsLocSku GetWcsLocSkuByLocId(long? locId)
    {
        var db = Db.Default;
        var result = db.WcsLocSkus.Where(t => t.LocId == locId).FirstOrDefault();
        return result;
    }
    public POWcsLocSku GetWcsLocSkuNoTaskByLocId(long? locId)
    {
        var db = Db.Default;
        var result = db.WcsLocSkus.Where(t => t.LocId == locId && t.TaskId == null).FirstOrDefault();
        return result;
    }


    public int AddWcsLocSku(long? locId, long? skuId)
    {
        if (locId == 0 || skuId == 0)
        {
            return 0;
        }
        var db = Db.Default;
        var result = db.WcsLocSkus.Insert(new POWcsLocSku
        {
            Id = XID.Nextval,
            LocId = locId,
            SkuId = skuId,
            SkuIndex = 1,
        });
        return result;
    }

    public int DeleteWcsLocSkuBySkuId(long? skuId)
    {
        if (skuId == 0)
        {
            return 0;
        }
        var db = Db.Default;
        //OLDTODO 这个删除要改
        var result = db.WcsLocSkus.Where(t => t.SkuId == skuId).Delete();
        return result;
    }
    public int DeleteWcsLocSkuByLoc(long? locId)
    {
        var db = Db.Default;
        var result = db.WcsLocSkus.Where(t => t.LocId == locId).Delete();
        return result;
    }


    public int SetWcsLocSku(long? locId, long? skuId, bool isDelTarget = false)
    {
        if (isDelTarget)
        {
            DeleteWcsLocSkuByLoc(locId);
        }
        var update = DeleteWcsLocSkuBySkuId(skuId);
        var add = AddWcsLocSku(locId, skuId);
        return update & add;
    }

    public int UpdateWcsLocSkuTask(long skuId, long taskId)
    {
        var db = Db.Default;
        var result = db.WcsLocSkus.Where(t => t.SkuId == skuId).Update( new POWcsLocSku
        {
            TaskId = taskId,
        });
        return result;
    }

    public List<StockMdTimeCount> GetStockMdTimeCount()
    {
        var db = Db.Default;
        var result = new List<StockMdTimeCount>();
        var prods = db.WcsLocSkus.Join(db.RcsLocations, f => f.LocId, s => s.Id, (f, s) => new
        {
            f.SkuId,
            s.LocType,
            s.LocHasSku,
            s.LocHasSkuTime,
        })
         .Where(t => t.LocType == (int)EnumLocType.Location && t.LocHasSku == 1)
         .Join(db.ProdSkuProducts, f => f.SkuId, s => s.SkuId, (f, s) => new
         {
             f.LocHasSkuTime,
             s.ProductId,
         }).Join(db.ProdProducts, f => f.ProductId, s => s.Id, (f, s) => new
         {
             MdTime = DateTime.Now.Subtract(s.MdTime ?? DateTime.Now).TotalHours,
             InTime = DateTime.Now.Subtract(f.LocHasSkuTime ?? DateTime.Now).TotalHours,
             s.ProductCode
         }).ToList();
        var time = 0.00;
        for (int i = 0; i < 10; i++)
        {
            var count = 0;
            var inCount = 0;
            var mdTime = "";
            if (time >= 72)
            {
                count = prods.Where(t => t.MdTime >= time).Count();
                inCount = prods.Where(t => t.InTime >= time).Count();
                mdTime = ">=72";
            }
            else
            {
                count = prods.Where(t => t.MdTime >= time && t.MdTime < (time + 8)).Count();
                inCount = prods.Where(t => t.InTime >= time && t.InTime < (time + 8)).Count();
                mdTime = $"{time}~{time + 8}(不含)";
            }

            var product = new StockMdTimeCount()
            {
                MdTime = mdTime,
                Count = count,
                InCount = inCount,
            };
            result.Add(product);
            time = time + 8;
        }
        return result;
    }

}
